﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace YArchitech.Revit.MEP.Calculator
{
	public class StatisticsItem
	{
		public static List<StatisticsData> CountEquipment(Autodesk.Revit.DB.Document ui_doc)
		{
			StatisticsItem.ui_Doc = ui_doc;
			List<Element> elems = new List<Element>();
			IList<Element> elems2 = StatisticsItem.FilterEquipment<FamilyInstance>(ui_doc, ref elems);
			List<StatisticsData> list = new List<StatisticsData>();
			list.AddRange(StatisticsItem.FilterElementByLoop(elems));
			list.AddRange(StatisticsItem.FilterElement(elems2));
			return list;
		}

		public static List<StatisticsData> FilterElementByLoop(IList<Element> elems)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(StatisticsItem.ui_Doc);
			filteredElementCollector.OfClass(typeof(ElectricalDemandFactorDefinition));
			int num = 1;
			List<StatisticsData> list = new List<StatisticsData>();
			double num2 = 0.0;
			new List<string>();
			foreach (Element element in elems)
			{
				num2 = 0.0;
				FamilyInstance familyInstance = element as FamilyInstance;
				if (familyInstance.MEPModel != null && familyInstance.MEPModel.ElectricalSystems != null)
				{
					bool flag = true;
					double needFactor = 1.0;
					double powerFactor = 0.8;
					if (element.GetParameter("光源功率") != null && element.GetParameter("光源数量") != null)
					{
						num2 = element.GetParameter("光源功率").AsDouble() * (double)element.GetParameter("光源数量").AsInteger();
						num2 = StatisticsItem.GetPowerData(num2);
						flag = false;
					}
					if (flag)
					{
						if (familyInstance.Symbol.GetParameter("功率") != null)
						{
							num2 = familyInstance.Symbol.GetParameter("功率").AsDouble();
						}
						num2 = StatisticsItem.GetPowerData(num2);
					}
					if (familyInstance.Symbol.GetParameter("功率系数") != null)
					{
						powerFactor = familyInstance.Symbol.GetParameter("功率系数").AsDouble();
					}
					if (familyInstance.Symbol.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM) != null)
					{
						needFactor = familyInstance.Symbol.GetParameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM).AsDouble();
					}
					string text = null;
					if (num2 > 0.0)
					{
						foreach (object obj in familyInstance.MEPModel.ElectricalSystems)
						{
							ElectricalSystem electricalSystem = (ElectricalSystem)obj;
							double voltage = 0.0;
							if (electricalSystem.BaseEquipment != null)
							{
								FamilyInstance baseEquipment = electricalSystem.BaseEquipment;
								StatisticsData statisticsData = new StatisticsData();
								text = baseEquipment.Name + electricalSystem.Name;
								ElementId elementId = familyInstance.Symbol.GetParameter("负荷分类").AsElementId();
								if (familyInstance.Symbol.GetParameter("电压") != null)
								{
									voltage = familyInstance.Symbol.GetParameter("电压").AsDouble();
								}
								else
								{
									voltage = 220.0;
								}
								ElectricalLoadClassification electricalLoadClassification = StatisticsItem.ui_Doc.GetElementById(elementId) as ElectricalLoadClassification;
								ElectricalDemandFactorDefinition electricalDemandFactorDefinition = null;
								foreach (Element element2 in filteredElementCollector.ToElements())
								{
									if (element2.Name == electricalLoadClassification.Name)
									{
										electricalDemandFactorDefinition = (element2 as ElectricalDemandFactorDefinition);
									}
								}
								if (electricalDemandFactorDefinition != null)
								{
									foreach (ElectricalDemandFactorValue electricalDemandFactorValue in electricalDemandFactorDefinition.GetValues())
									{
										if (electricalDemandFactorValue.Factor > 0.0)
										{
											needFactor = electricalDemandFactorValue.Factor;
										}
									}
								}
								if (list.Count == 0)
								{
									list.Add(new StatisticsData
									{
										EquipmentAndLoop = text,
										Phase = StatisticsItem.GetEquipmentValue(baseEquipment),
										Voltage = voltage,
										CatagoryName = electricalLoadClassification.Name,
										EquipPower = num2,
										PowerFactor = powerFactor,
										NeedFactor = needFactor
									});
									num++;
								}
								else if (!StatisticsItem.isContainer(list, electricalSystem, familyInstance))
								{
									list.Add(new StatisticsData
									{
										EquipmentAndLoop = text,
										Num = num,
										Phase = StatisticsItem.GetEquipmentValue(baseEquipment),
										Voltage = voltage,
										CatagoryName = electricalLoadClassification.Name,
										EquipPower = num2,
										PowerFactor = powerFactor,
										NeedFactor = needFactor
									});
									num++;
								}
								else
								{
									foreach (StatisticsData statisticsData2 in list)
									{
										if (statisticsData2.EquipmentAndLoop == text && statisticsData2.Phase == StatisticsItem.GetEquipmentValue(baseEquipment))
										{
											statisticsData2.EquipPower += num2;
										}
									}
								}
							}
						}
					}
				}
			}
			return list;
		}

		public static double Getvoltage(double revit)
		{
			return Math.Round(revit / 10.7639104167079, MidpointRounding.AwayFromZero);
		}

		public static bool isContainer(List<StatisticsData> list, ElectricalSystem esItem, FamilyInstance fi)
		{
			FamilyInstance baseEquipment = esItem.BaseEquipment;
			string b = baseEquipment.Name + esItem.Name;
			if (fi.Symbol.GetParameter("电压") != null)
			{
				fi.Symbol.GetParameter("电压").AsDouble();
			}
			foreach (StatisticsData statisticsData in list)
			{
				if (statisticsData.EquipmentAndLoop == b && statisticsData.Phase == StatisticsItem.GetEquipmentValue(baseEquipment))
				{
					return true;
				}
			}
			return false;
		}

		public static bool isContainer(List<StatisticsData> list, FamilyInstance familyInstance, string str)
		{
			if (familyInstance.Symbol.GetParameter("电压") != null)
			{
				familyInstance.Symbol.GetParameter("电压").AsDouble();
			}
			foreach (StatisticsData statisticsData in list)
			{
				if (statisticsData.CatagoryName == str && statisticsData.Phase == StatisticsItem.GetEquipmentValue(familyInstance))
				{
					return true;
				}
			}
			return false;
		}

		public static string GetEquipmentValue(FamilyInstance familyInstance)
		{
			string result;
			try
			{
				double num = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEA).AsDouble();
				double num2 = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEB).AsDouble();
				double num3 = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEC).AsDouble();
				if (num > 0.0 && num2 > 0.0 && num3 > 0.0)
				{
					result = "3";
				}
				else
				{
					result = "1";
				}
			}
			catch (Exception)
			{
				result = "1";
			}
			return result;
		}

		public static List<StatisticsData> FilterElement(IList<Element> elems)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(StatisticsItem.ui_Doc);
			filteredElementCollector.OfClass(typeof(ElectricalDemandFactorDefinition));
			double num = 0.0;
			int num2 = 1;
			List<StatisticsData> list = new List<StatisticsData>();
			List<string> list2 = new List<string>();
			foreach (Element element in elems)
			{
				num = 0.0;
				FamilyInstance familyInstance = element as FamilyInstance;
				bool flag = true;
				double needFactor = 1.0;
				double powerFactor = 0.8;
				double voltage = 0.0;
				if (element.GetParameter("光源功率") != null && element.GetParameter("光源数量") != null)
				{
					num = element.GetParameter("光源功率").AsDouble() * (double)element.GetParameter("光源数量").AsInteger();
					flag = false;
				}
				if (flag && familyInstance.Symbol.GetParameter("功率") != null)
				{
					num = familyInstance.Symbol.GetParameter("功率").AsDouble();
				}
				num = StatisticsItem.GetPowerData(num);
				if (familyInstance.Symbol.GetParameter("功率系数") != null)
				{
					powerFactor = familyInstance.Symbol.GetParameter("功率系数").AsDouble();
				}
				if (familyInstance.Symbol.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM) != null)
				{
					needFactor = familyInstance.Symbol.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM).AsDouble();
				}
				string text = null;
				if (num > 0.0)
				{
					if (familyInstance.Symbol.GetParameter("负荷分类") == null)
					{
						text = "其他";
						ElectricalDemandFactorDefinition electricalDemandFactorDefinition = null;
						foreach (Element element2 in filteredElementCollector.ToElements())
						{
							if (element2.Name == text)
							{
								electricalDemandFactorDefinition = (element2 as ElectricalDemandFactorDefinition);
							}
						}
						if (electricalDemandFactorDefinition != null)
						{
							foreach (ElectricalDemandFactorValue electricalDemandFactorValue in electricalDemandFactorDefinition.GetValues())
							{
								if (electricalDemandFactorValue.Factor > 0.0)
								{
									needFactor = electricalDemandFactorValue.Factor;
								}
							}
						}
						if (!list2.Contains("其他"))
						{
							list2.Add(text);
							list.Add(new StatisticsData
							{
								CatagoryName = "其他",
								EquipPower = num,
								PowerFactor = powerFactor,
								NeedFactor = needFactor
							});
							continue;
						}
					}
					else
					{
						ElementId elementId = familyInstance.Symbol.GetParameter("负荷分类").AsElementId();
						if (familyInstance.Symbol.GetParameter("电压") != null)
						{
							voltage = familyInstance.Symbol.GetParameter("电压").AsDouble();
						}
						else
						{
							voltage = 220.0;
						}
						text = (StatisticsItem.ui_Doc.GetElementById(elementId) as ElectricalLoadClassification).Name;
						ElectricalDemandFactorDefinition electricalDemandFactorDefinition2 = null;
						foreach (Element element3 in filteredElementCollector.ToElements())
						{
							if (element3.Name == text)
							{
								electricalDemandFactorDefinition2 = (element3 as ElectricalDemandFactorDefinition);
							}
						}
						if (electricalDemandFactorDefinition2 != null)
						{
							foreach (ElectricalDemandFactorValue electricalDemandFactorValue2 in electricalDemandFactorDefinition2.GetValues())
							{
								if (electricalDemandFactorValue2.Factor > 0.0)
								{
									needFactor = electricalDemandFactorValue2.Factor;
								}
							}
						}
					}
					if (list2.Count == 0)
					{
						list2.Add(text);
						list.Add(new StatisticsData
						{
							Phase = StatisticsItem.GetEquipmentValue(familyInstance),
							Voltage = voltage,
							CatagoryName = text,
							EquipPower = num,
							PowerFactor = powerFactor,
							NeedFactor = needFactor
						});
						num2++;
					}
					else if (!StatisticsItem.isContainer(list, familyInstance, text))
					{
						list2.Add(text);
						list.Add(new StatisticsData
						{
							Num = num2,
							Phase = StatisticsItem.GetEquipmentValue(familyInstance),
							Voltage = voltage,
							CatagoryName = text,
							EquipPower = num,
							PowerFactor = powerFactor,
							NeedFactor = needFactor
						});
						num2++;
					}
					else
					{
						foreach (StatisticsData statisticsData in list)
						{
							if (statisticsData.CatagoryName == text && statisticsData.Phase == StatisticsItem.GetEquipmentValue(familyInstance))
							{
								statisticsData.EquipPower += num;
							}
						}
					}
				}
			}
			return list;
		}

		public static IList<Element> FilterEquipment<T>(Autodesk.Revit.DB.Document doc, ref List<Element> listelem)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(T));
			IEnumerable<Element> enumerable = filteredElementCollector.ToElements();
			List<Element> list = new List<Element>();
			foreach (Element element in enumerable)
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				try
				{
					if (familyInstance.MEPModel != null)
					{
						if (familyInstance.MEPModel.ElectricalSystems != null)
						{
							listelem.Add(element);
						}
						else if (familyInstance.MEPModel.ConnectorManager != null)
						{
							if (familyInstance.MEPModel.ConnectorManager.Connectors != null)
							{
								if (familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator() != null)
								{
									if (familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator().MoveNext())
									{
										list.Add(element);
									}
								}
							}
						}
					}
				}
				catch (Exception)
				{
				}
			}
			return list;
		}

		public static double GetPowerData(double power)
		{
			return power * 0.0929031299;
		}

		private static Document ui_Doc;
	}
}
